package com.lcj.demo.web;

import com.lcj.demo.domain.Employee;
import com.lcj.demo.domain.Job;
import com.lcj.demo.service.EmployeeService;
import com.lcj.demo.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "员工", description = "员工相关操作Api")
@RestController
@RequestMapping("/api")
public class EmployeeController {
    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @ApiOperation(value="获取所有员工", notes="获取所有员工")
    @GetMapping("/employees")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @ApiOperation(value="根据关键字查询员工", notes="根据关键字查询员工")
    @ApiImplicitParam(name = "searchKey", value = "关键字", required = true, dataType = "String")
    @GetMapping("/employees/search")
    public List<Employee> findAllBySearchKey(String searchKey) {
        if(searchKey == null ||searchKey == ""){
            return employeeService.findAll();
        }
        return employeeService.findAllBySearchKey(searchKey);
    }

    @ApiOperation(value="创建员工", notes="创建员工")
    @ApiImplicitParam(name = "Employee", value = "详细实体", required = true, dataType = "Employee")
    @PostMapping("/employee")
    public Employee create(@RequestBody Employee job) {
        return employeeService.saveOrUpdate(job);
    }

    @ApiOperation(value="更新员工信息", notes="更新员工信息")
    @ApiImplicitParam(name = "Employee", value = "详细实体", required = true, dataType = "Employee")
    @PutMapping("/employee")
    public Employee update(@RequestBody Employee employee) {
        return employeeService.saveOrUpdate(employee);
    }

    @ApiOperation(value="删除员工信息", notes="删除员工信息")
    @ApiImplicitParam(name = "ID", value = "员工id", required = true, dataType = "Long")
    @DeleteMapping("/employee/{id}")
    public void delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
    }

    @ApiOperation(value="根据id查找用户", notes="根据id查找用户")
    @ApiImplicitParam(name = "ID", value = "员工id", required = true, dataType = "Long")
    @GetMapping("/employee")
    public Employee findById(Long id) {
        return employeeService.findById(id).orElse(null);
    }

}
